﻿#include "TcpCodec_User.h"

#include "Mapper_Device.h"
#include "RegisterCenter_Device.h"
#include "Device.h"

TcpCodec_User::TcpCodec_User()
{
  setObjectName(getClassNameFirstLow<TcpCodec_User>());
}

TcpCodec_User::~TcpCodec_User()
{}

DataPackage_TcpCodec* TcpCodec_User::decode(DataPackage_TcpCodec* dataPackageInfo_User)
{
  if(AllDevice==((dataPackageInfo_User->getData()[0])&0xff))
    {
      QList<uint> deviceList=mapper_Device->getDevicesByUserId(dataPackageInfo_User->getUserId());
      QByteArray result;
      result.append(AllDevice);
      for(int deviceId:deviceList)
        {
          if(registerCenter_Device->isOnline(deviceId))
            {
              result.append(registerCenter_Device->getDeviceById(deviceId)->toByteArray());
            }
          else
            {
              result.append(Device().setDeviceId(deviceId)->toByteArray());
            }
        }
      return dataPackageInfo_User->setData(result);
    }
  return Q_NULLPTR;
}

Mapper_Device* TcpCodec_User::getMapper_Device()const
{
  return mapper_Device;
}

TcpCodec_User* TcpCodec_User::setMapper_Device(Mapper_Device* const mapper_Device)
{
  this->mapper_Device=mapper_Device;
  printInjection(this,mapper_Device);
  return this;
}

RegisterCenter_Device* TcpCodec_User::getRegisterCenter_Device()const
{
  return registerCenter_Device;
}

TcpCodec_User* TcpCodec_User::setRegisterCenter_Device(RegisterCenter_Device* const registerCenter_Device)
{
  this->registerCenter_Device=registerCenter_Device;
  printInjection(this,registerCenter_Device);
  return this;
}

void TcpCodec_User::objectAutoInjection(QHash<QString,QObject*>* objMap)
{
  setMapper_Device(getObjFromMap<Mapper_Device>(objMap));
  setRegisterCenter_Device(getObjFromMap<RegisterCenter_Device>(objMap));
}
